package com.msb.serviceDriverUser.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.constant.DriverCarConstants;
import com.msb.internalcommon.dto.*;
import com.msb.internalcommon.reponese.OrderDriverResponse;
import com.msb.serviceDriverUser.mapper.CarMapper;
import com.msb.serviceDriverUser.mapper.DriverCarBindingRelationshipMapper;
import com.msb.serviceDriverUser.mapper.DriverUserMapper;
import com.msb.serviceDriverUser.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: jiudianliu
 * @Date: 2023/6/22 - 06 - 22 - 15:13
 * @Description: com.msb.serviceDriverUser.service
 * @version: 1.0
 */
@Service
public class DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper ;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper ;

    @Autowired
    private CarMapper carMapper;

    public ResponseResult addDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);

        //初始化 司机工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return ResponseResult.success("");
    }

    public ResponseResult updateDriverUser(DriverUser driverUser){
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    /**
     *  查询司机手机号
     * @param driverPhone
     * @return
     */
    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone){
        Map<String,Object> map = new HashMap<>() ;
        map.put("driver_phone" , driverPhone) ;
        map.put("state" , DriverCarConstants.DRIVER_STATE_VALID) ;
        List<DriverUser> driverUsers = driverUserMapper.selectByMap(map) ;
        if (driverUsers.isEmpty()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXISTS.getCode() , CommonStatusEnum.DRIVER_NOT_EXISTS.getValue()) ;
        }
        DriverUser driverUser = driverUsers.get(0);
        return ResponseResult.success(driverUser) ;
    }

    /**
     *
     * @param carId
     * @return
     */
    @Autowired
    DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper ;

    public ResponseResult<OrderDriverResponse> getAvailableDriver(Long carId){
        // 车辆与司机 绑定关系 的查询
        QueryWrapper<DriverCarBindingRelationship> DriverCarBindingRelationshipQueryWrapper = new QueryWrapper<>();
        DriverCarBindingRelationshipQueryWrapper.eq("car_id" , carId);
        DriverCarBindingRelationshipQueryWrapper.eq("bind_state" , DriverCarConstants.DRIVER_CAR_BIND);

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(DriverCarBindingRelationshipQueryWrapper);
        Long driverId = driverCarBindingRelationship.getDriverId();


        QueryWrapper<DriverUserWorkStatus> DriverUserWorkStatusQueryWrapper = new QueryWrapper<>();
        DriverUserWorkStatusQueryWrapper.eq("driver_id" , driverId);
        DriverUserWorkStatusQueryWrapper.eq("work_status" , DriverCarConstants.DRIVER_WORK_STATUS_START);

        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatusMapper.selectOne(DriverUserWorkStatusQueryWrapper);
        if (driverUserWorkStatus == null){
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getCode() , CommonStatusEnum.AVAILABLE_DRIVER_EMPTY.getValue());
        }else{

            // 查询 司机电话号码
            QueryWrapper<DriverUser> DriverUserQueryWrapper = new QueryWrapper<>();
            DriverUserQueryWrapper.eq("id" , driverId);
            DriverUser driverUser = driverUserMapper.selectOne(DriverUserQueryWrapper);

            //  查询车辆信息
            QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
            carQueryWrapper.eq("id" , carId);
            Car car = carMapper.selectOne(carQueryWrapper);


            OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
            orderDriverResponse.setCarId(carId);
            orderDriverResponse.setDriverId(driverId);
            orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());


            orderDriverResponse.setLicenseId(driverUser.getLicenseId());
            orderDriverResponse.setVehicleNo(car.getVehicleNo());



            return ResponseResult.success(orderDriverResponse);
            
        }

    }
}
